/*
 * Academic License - for use in teaching, academic research, and meeting
 * course requirements at degree granting institutions only.  Not for
 * government, commercial, or other organizational use.
 *
 * sparse.c
 *
 * Code generation for function 'sparse'
 *
 */

/* Include files */
#include "sparse.h"
#include "emxutil.h"
#include "types.h"
#include "rt_nonfinite.h"

/* Function Definitions */
int sparse(double varargin_1, double varargin_2, double varargin_4,
           double varargin_5, emxArray_real_T *y_d, emxArray_int32_T *y_colidx,
           emxArray_int32_T *y_rowidx, int *y_n)
{
  double *y_d_data;
  int c;
  int cptr;
  int i;
  int ridx;
  int y_m;
  int *y_colidx_data;
  int *y_rowidx_data;
  ridx = (int)varargin_5;
  i = y_d->size[0];
  y_d->size[0] = 1;
  emxEnsureCapacity_real_T(y_d, i);
  y_d_data = y_d->data;
  i = y_colidx->size[0];
  y_colidx->size[0] = (int)varargin_5 + 1;
  emxEnsureCapacity_int32_T(y_colidx, i);
  y_colidx_data = y_colidx->data;
  y_colidx_data[0] = 1;
  i = y_rowidx->size[0];
  y_rowidx->size[0] = 1;
  emxEnsureCapacity_int32_T(y_rowidx, i);
  y_rowidx_data = y_rowidx->data;
  y_rowidx_data[0] = 0;
  cptr = 1;
  for (c = 0; c < ridx; c++) {
    while ((cptr <= 1) && ((int)varargin_2 == c + 1)) {
      y_rowidx_data[0] = (int)varargin_1;
      cptr = 2;
    }
    y_colidx_data[c + 1] = cptr;
  }
  y_d_data[0] = -1.0;
  cptr = 1;
  i = y_colidx->size[0];
  for (c = 0; c <= i - 2; c++) {
    ridx = y_colidx_data[c];
    y_colidx_data[c] = cptr;
    int exitg1;
    int i1;
    do {
      exitg1 = 0;
      i1 = y_colidx_data[c + 1];
      if (ridx < i1) {
        double val;
        int currRowIdx;
        val = 0.0;
        currRowIdx = y_rowidx_data[ridx - 1];
        while ((ridx < i1) && (y_rowidx_data[ridx - 1] == currRowIdx)) {
          val += y_d_data[ridx - 1];
          ridx++;
        }
        if (val != 0.0) {
          y_d_data[cptr - 1] = val;
          y_rowidx_data[cptr - 1] = currRowIdx;
          cptr++;
        }
      } else {
        exitg1 = 1;
      }
    } while (exitg1 == 0);
  }
  y_colidx_data[y_colidx->size[0] - 1] = cptr;
  y_m = (int)varargin_4;
  *y_n = (int)varargin_5;
  return y_m;
}

/* End of code generation (sparse.c) */
